/*/{Protheus.doc} SYNCDAS
E a User Function de geracao do layout DAS (Documentos Associados) do Sistema Synchro
@author Elton Teodoro Alves
@since 14/11/2014
@version P11 R5
@Param aParam,Array,Array enviado pelo schudele
@Example
Esta User Function foi desenvolvida para ser utilizada com o Schudele, que passa como parametro para User Function um array com a seguinte configuracao:</br>
</br>
<table>
<tr><th> POSICAO </th><th> TIPO </th><th> DESCRICAO             </th></tr>
<tr><td> [01]    </td><td> C    </td><td> Codigo da Empresa     </td></tr>
<tr><td> [02]    </td><td> C    </td><td> Codigo da Filial      </td></tr>
<tr><td> [03]    </td><td> C    </td><td> Codigo do Agendamento </td></tr>
<tr><td> [04]    </td><td> C    </td><td> Codigo da Tarefa      </td></tr>
</table>
</br>
Para teste fora do schudele, pode-se enviar para o SmartClient com a clausula -a estes dados separadas por virgula ',' Ex.: 99,01,0001,0001
/*/

#Define CRLF Chr(13) + Chr(10)
Static PERIOD := 'D'

#Include 'Protheus.ch'

User Function SYNCDAS(xParam)
	
	Local	cLayout		:=	Right(ProcName(),3)
	Local	aParam		:=	If(Valtype(xParam) == 'C',Separa(xParam,',',.T.),xParam)
	Local	aSyncEnv	:=	U_SYNCENV(aParam,cLayout,PERIOD)
	Local	cTrab		:=	GetNextAlias()
	Local	cTempTrab	:=	cLayout + Dtos(Date()) + StrTran(Time(),':','')
	Local	cTempFile	:=	GetSrvProfString("StartPath","") + cTempTrab + GetDbExtension()
	Local	cQuerySF1		:=	''
	Local	cQuerySF2		:=	''
	Local	cQuery		:=	''
	Local	cQueryBkp	:=	''
	Local	aStru		:=	{}
	Local	nX			:=	0
	Local	aFiliais	:=	StrTokArr(AllTrim(aSyncEnv[10]),'/',)
	Local	cFiliais	:=	''
	Local	nCount		:=	0
	Local	SF1Emissao	:=	''
	
	// Monta a string com a lista de filiais para ser utilizada na query
	
	For nX := 1 To Len(aFiliais)
		
		cFiliais += "'" + aFiliais[nX] + "'"
		
		If nX # Len(aFiliais)
			
			cFiliais += ","
			
		End If
		
	Next nX
	
	//Variavel que monta o case que busca a coreeta data de emissao da NF de entrada de origem
	SF1Emissao	+=	"(CASE SF1_ORI.F1_FORMUL " + CRLF
	SF1Emissao	+=	"WHEN 'N' THEN SF1_ORI.F1_DTDIGIT " + CRLF
	SF1Emissao	+=	"ELSE SF1_ORI.F1_DAUTNFE END)" + CRLF
	
	// --------------------------------------------------------------
	// Monta estrutura do arquivo temporario com dados a serem exportados
	// --------------------------------------------------------------
	aAdd(aStru,{'POS0001','C',0005,0}) // EDOF_CODIGO
	aAdd(aStru,{'POS0006','C',0001,0}) // CTRL_INSTRUCAO
	aAdd(aStru,{'POS0007','C',0008,0}) // SERIE_SUBSERIE_DOC_ASSOC
	aAdd(aStru,{'POS0015','C',0008,0}) // SERIE_DOC_ASSOC
	aAdd(aStru,{'POS0023','C',0008,0}) // SUB_SERIE_DOC_ASSOC
	aAdd(aStru,{'POS0031','C',0008,0}) // NUM_DOC_ASSOC
	aAdd(aStru,{'POS0039','C',0008,0}) // DT_DOC
	aAdd(aStru,{'POS0047','C',0040,0}) // DOF_IMPORT_NUMERO_ASSOC
	aAdd(aStru,{'POS0087','C',0040,0}) // DOF_IMPORT_NUMERO
	aAdd(aStru,{'POS0127','C',0018,0}) // INFORMANTE_EST_CODIGO
	aAdd(aStru,{'POS0145','C',0018,0}) // PFJ_EMITENTE_ASSOC
	aAdd(aStru,{'POS0163','C',0018,0}) // PFJ_DESTINATARIO_ASSOC
	aAdd(aStru,{'POS0181','C',0021,0}) // VL_CONTABIL_DOC
	
	// --------------------------------------------------------------
	
	If aSyncEnv[6]
		
		// --------------------------------------------------------------
		// Montagem da query de pesquisa
		// --------------------------------------------------------------
		cQuerySF1	+=	"SELECT DISTINCT " + CRLF
		cQuerySF2	+=	"SELECT DISTINCT " + CRLF
		
		cQuerySF1	+=	"'SF1' ORIGEM," + CRLF
		cQuerySF2	+=	"'SF2' ORIGEM," + CRLF
		
		cQuerySF1	+=	"SF1.F1_FILIAL FILIAL," + CRLF
		cQuerySF2	+=	"SF2.F2_FILIAL FILIAL," + CRLF
		
		cQuerySF1	+=	"SF1.R_E_C_N_O_ RECNO," + CRLF
		cQuerySF2	+=	"SF2.R_E_C_N_O_ RECNO," + CRLF
		
		cQuerySF1	+=	"SD1.D1_SERIORI SERIE_ORI," + CRLF
		cQuerySF2	+=	"SD2.D2_SERIORI SERIE_ORI," + CRLF
		
		cQuerySF1	+=	"SD1.D1_NFORI DOC_ORI," + CRLF
		cQuerySF2	+=	"SD2.D2_NFORI DOC_ORI," + CRLF
		
		cQuerySF1	+=	"SF1.F1_EMISSAO EMISSAO," + CRLF
		cQuerySF2	+=	"SF2.F2_EMISSAO EMISSAO," + CRLF
		
		cQuerySF1	+=	"SA1.A1_CGC SA1_CGC," + CRLF
		cQuerySF2	+=	"SA1.A1_CGC SA1_CGC," + CRLF
		
		cQuerySF1	+=	"SA2.A2_CGC SA2_CGC," + CRLF
		cQuerySF2	+=	"SA2.A2_CGC SA2_CGC," + CRLF
		
		cQuerySF1	+=	"SF1.F1_TIPO TIPO," + CRLF
		cQuerySF2	+=	"SF2.F2_TIPO TIPO," + CRLF
		
		cQuerySF1	+=	"SF1.F1_FORMUL FORMUL," + CRLF
		cQuerySF2	+=	"'S' FORMUL," + CRLF
		
		// Data de Emissao do Documento Original
		cQuerySF1	+=	"CASE SF1.F1_TIPO " + CRLF
		cQuerySF1	+=	"WHEN 'C' THEN " + CRLF + SF1Emissao + CRLF
		cQuerySF1	+=	"WHEN 'I' THEN " + CRLF + SF1Emissao + CRLF
		cQuerySF1	+=	"WHEN 'P' THEN " + CRLF + SF1Emissao + CRLF
		cQuerySF1	+=	"WHEN 'D' THEN SF2_ORI.F2_DAUTNFE" + CRLF
		cQuerySF1	+=	"ELSE ''" + CRLF
		cQuerySF1	+=	"END EMISSAO_ORI," + CRLF
		
		cQuerySF2	+=	"CASE SF2.F2_TIPO " + CRLF
		cQuerySF2	+=	"WHEN 'C' THEN SF2_ORI.F2_DAUTNFE" + CRLF
		cQuerySF2	+=	"WHEN 'I' THEN SF2_ORI.F2_DAUTNFE" + CRLF
		cQuerySF2	+=	"WHEN 'P' THEN SF2_ORI.F2_DAUTNFE" + CRLF
		cQuerySF2	+=	"WHEN 'D' THEN " + CRLF + SF1Emissao + CRLF
		cQuerySF2	+=	"ELSE ''" + CRLF
		cQuerySF2	+=	"END EMISSAO_ORI," + CRLF
		
		// RECNO Documento Original
		cQuerySF1	+=	"CASE SF1.F1_TIPO " + CRLF
		cQuerySF1	+=	"WHEN 'C' THEN SF1_ORI.R_E_C_N_O_" + CRLF
		cQuerySF1	+=	"WHEN 'I' THEN SF1_ORI.R_E_C_N_O_" + CRLF
		cQuerySF1	+=	"WHEN 'P' THEN SF1_ORI.R_E_C_N_O_" + CRLF
		cQuerySF1	+=	"WHEN 'D' THEN SF2_ORI.R_E_C_N_O_" + CRLF
		cQuerySF1	+=	"ELSE 0" + CRLF
		cQuerySF1	+=	"END RECNO_ORI," + CRLF
		
		cQuerySF2	+=	"CASE SF2.F2_TIPO " + CRLF
		cQuerySF2	+=	"WHEN 'C' THEN SF2_ORI.R_E_C_N_O_" + CRLF
		cQuerySF2	+=	"WHEN 'I' THEN SF2_ORI.R_E_C_N_O_" + CRLF
		cQuerySF2	+=	"WHEN 'P' THEN SF2_ORI.R_E_C_N_O_" + CRLF
		cQuerySF2	+=	"WHEN 'D' THEN SF1_ORI.R_E_C_N_O_" + CRLF
		cQuerySF2	+=	"ELSE 0" + CRLF
		cQuerySF2	+=	"END RECNO_ORI," + CRLF
		
		//------------------------------------------------
		
		cQuerySF1	+=	"ZB6.ZB6_TNSYNC POS0001," + CRLF
		cQuerySF2	+=	"ZB6.ZB6_TNSYNC POS0001," + CRLF
		
		cQuerySF1	+=	"'I' POS0006," + CRLF
		cQuerySF2	+=	"'I' POS0006," + CRLF
		
		
		cQuerySF1	+=	"SD1.D1_SERIE POS0007," + CRLF
		cQuerySF2	+=	"SD2.D2_SERIE POS0007," + CRLF
		
		cQuerySF1	+=	"'' POS0015," + CRLF
		cQuerySF2	+=	"'' POS0015," + CRLF
		
		cQuerySF1	+=	"'' POS0023," + CRLF
		cQuerySF2	+=	"'' POS0023," + CRLF
		
		cQuerySF1	+=	"SD1.D1_DOC POS0031," + CRLF
		cQuerySF2	+=	"SD2.D2_DOC POS0031," + CRLF
		
		//------ DATA DE EMISSAO DA NF SF1 SF2
		
		cQuerySF1	+=	"CASE SF1.F1_FORMUL " + CRLF
		cQuerySF1	+=	"WHEN 'N' THEN SF1.F1_DTDIGIT" + CRLF
		cQuerySF1	+=	"ELSE SF1.F1_DAUTNFE END POS0039," + CRLF
		
		cQuerySF2	+=	"SF2.F2_DAUTNFE POS0039," + CRLF
		
		//------
		
		cQuerySF1	+=	"'' POS0047," + CRLF
		cQuerySF2	+=	"'' POS0047," + CRLF
		
		cQuerySF1	+=	"'' POS0087," + CRLF
		cQuerySF2	+=	"'' POS0087," + CRLF
		
		cQuerySF1	+=	"'' POS0127," + CRLF
		cQuerySF2	+=	"'' POS0127," + CRLF
		
		cQuerySF1	+=	"'' POS0145," + CRLF
		cQuerySF2	+=	"'' POS0145," + CRLF
		
		cQuerySF1	+=	"'' POS0163," + CRLF
		cQuerySF2	+=	"'' POS0163," + CRLF
		
		cQuerySF1	+=	"SF1.F1_VALBRUT POS0181" + CRLF
		cQuerySF2	+=	"SF2.F2_VALBRUT POS0181" + CRLF
		
		//---------- FROM SF1 SF2
		
		cQuerySF1	+=	"FROM " + RetSqlName("SF1") + " SF1 " + CRLF
		cQuerySF2	+=	"FROM " + RetSqlName("SF2") + " SF2 " + CRLF
		
		//---------- LEFT JOIN SF1
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("SD1") + " SD1 ON " + CRLF
		cQuerySF1	+=	"SF1.F1_FILIAL = SD1.D1_FILIAL AND " + CRLF
		cQuerySF1	+=	"SF1.F1_SERIE = SD1.D1_SERIE AND " + CRLF
		cQuerySF1	+=	"SF1.F1_DOC = SD1.D1_DOC AND " + CRLF
		cQuerySF1	+=	"SF1.F1_EMISSAO = SD1.D1_EMISSAO AND " + CRLF
		cQuerySF1	+=	"SF1.F1_FORNECE = SD1.D1_FORNECE AND " + CRLF
		cQuerySF1	+=	"SF1.F1_LOJA	= SD1.D1_LOJA " + CRLF
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("SF1") + " SF1_ORI ON " + CRLF
		cQuerySF1	+=	"SD1.D1_FILIAL = SF1_ORI.F1_FILIAL AND " + CRLF
		cQuerySF1	+=	"SD1.D1_SERIORI = SF1_ORI.F1_SERIE AND " + CRLF
		cQuerySF1	+=	"SD1.D1_NFORI = SF1_ORI.F1_DOC AND " + CRLF
		cQuerySF1	+=	"SD1.D1_DATORI = SF1_ORI.F1_EMISSAO AND " + CRLF
		cQuerySF1	+=	"SD1.D1_FORNECE = SF1_ORI.F1_FORNECE AND " + CRLF
		cQuerySF1	+=	"SD1.D1_LOJA = SF1_ORI.F1_LOJA " + CRLF
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("SF2") + " SF2_ORI ON " + CRLF
		cQuerySF1	+=	"SD1.D1_FILIAL = SF2_ORI.F2_FILIAL AND " + CRLF
		cQuerySF1	+=	"SD1.D1_SERIORI = SF2_ORI.F2_SERIE AND " + CRLF
		cQuerySF1	+=	"SD1.D1_NFORI = SF2_ORI.F2_DOC AND " + CRLF
		cQuerySF1	+=	"SD1.D1_DATORI = SF2_ORI.F2_EMISSAO AND " + CRLF
		cQuerySF1	+=	"SD1.D1_FORNECE = SF2_ORI.F2_CLIENTE AND " + CRLF
		cQuerySF1	+=	"SD1.D1_LOJA = SF2_ORI.F2_LOJA " + CRLF
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("ZB6") + " ZB6 " + CRLF
		cQuerySF1	+=	"ON ZB6.ZB6_ENTSAI = 'E' AND " + CRLF
		cQuerySF1	+=	"ZB6.ZB6_FILIAL = SF1.F1_FILIAL AND " + CRLF
		cQuerySF1	+=	"ZB6.ZB6_TPDOCT = SF1.F1_ESPECIE " + CRLF
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("SA1") + " SA1 " + CRLF
		cQuerySF1	+=	"ON SF1.F1_FORNECE = SA1.A1_COD AND " + CRLF
		cQuerySF1	+=	"SF1.F1_LOJA = SA1.A1_LOJA " + CRLF
		
		cQuerySF1	+=	"LEFT JOIN " + RetSqlName("SA2") + " SA2 " + CRLF
		cQuerySF1	+=	"ON SF1.F1_FORNECE = SA2.A2_COD AND " + CRLF
		cQuerySF1	+=	"SF1.F1_LOJA = SA2.A2_LOJA " + CRLF
		
		//---------- WHERE SF1
		
		cQuerySF1	+=	"WHERE (SF1.F1_DTDIGIT = '" + aSyncEnv[9] + "' OR SF1.F1_DAUTNFE = '" + aSyncEnv[9] + "') AND " + CRLF
		cQuerySF1	+=	"SF1.F1_FILIAL IN(" + cFiliais + ") AND " + CRLF
		cQuerySF1	+=	"(SD1.D1_SERIORI <> '" + Space(GetSx3Cache('D1_SERIORI','X3_TAMANHO')) + "' OR " + CRLF
		cQuerySF1	+=	"SD1.D1_NFORI <> '" + Space(GetSx3Cache('D1_NFORI','X3_TAMANHO')) + "' OR " + CRLF
		cQuerySF1	+=	"SD1.D1_DATORI <> '" + Space(GetSx3Cache('D1_DATORI','X3_TAMANHO')) + "') AND " + CRLF
		cQuerySF1	+=	"SF1.F1_TIPO IN('C','I','P','D') AND " + CRLF
		cQuerySF1	+=	"ISNULL(SF1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(SD1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(SF1_ORI.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(SF2_ORI.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(ZB6.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(SA1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF1	+=	"ISNULL(SA2.D_E_L_E_T_,'') <> '*' " + CRLF
		
		//---------- LEFT JOIN SF2
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("SD2") + " SD2 ON " + CRLF
		cQuerySF2	+=	"SF2.F2_FILIAL = SD2.D2_FILIAL AND " + CRLF
		cQuerySF2	+=	"SF2.F2_SERIE = SD2.D2_SERIE AND " + CRLF
		cQuerySF2	+=	"SF2.F2_DOC = SD2.D2_DOC AND " + CRLF
		cQuerySF2	+=	"SF2.F2_EMISSAO = SD2.D2_EMISSAO AND " + CRLF
		cQuerySF2	+=	"SF2.F2_CLIENTE = SD2.D2_CLIENTE AND " + CRLF
		cQuerySF2	+=	"SF2.F2_LOJA	= SD2.D2_LOJA " + CRLF
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("SF1") + " SF1_ORI ON " + CRLF
		cQuerySF2	+=	"SD2.D2_FILIAL = SF1_ORI.F1_FILIAL AND " + CRLF
		cQuerySF2	+=	"SD2.D2_SERIORI = SF1_ORI.F1_SERIE AND " + CRLF
		cQuerySF2	+=	"SD2.D2_NFORI = SF1_ORI.F1_DOC AND " + CRLF
		cQuerySF2	+=	"SD2.D2_CLIENTE = SF1_ORI.F1_FORNECE AND " + CRLF
		cQuerySF2	+=	"SD2.D2_LOJA = SF1_ORI.F1_LOJA " + CRLF
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("SF2") + " SF2_ORI ON " + CRLF
		cQuerySF2	+=	"SD2.D2_FILIAL = SF2_ORI.F2_FILIAL AND " + CRLF
		cQuerySF2	+=	"SD2.D2_SERIORI = SF2_ORI.F2_SERIE AND " + CRLF
		cQuerySF2	+=	"SD2.D2_NFORI = SF2_ORI.F2_DOC AND " + CRLF
		cQuerySF2	+=	"SD2.D2_CLIENTE = SF2_ORI.F2_CLIENTE AND " + CRLF
		cQuerySF2	+=	"SD2.D2_LOJA = SF2_ORI.F2_LOJA " + CRLF
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("ZB6") + " ZB6 " + CRLF
		cQuerySF2	+=	"ON ZB6.ZB6_ENTSAI = 'S' AND " + CRLF
		cQuerySF2	+=	"ZB6.ZB6_FILIAL = SF2.F2_FILIAL AND " + CRLF
		cQuerySF2	+=	"ZB6.ZB6_SERIE = SF2.F2_SERIE " + CRLF
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("SA1") + " SA1 " + CRLF
		cQuerySF2	+=	"ON SF2.F2_CLIENTE = SA1.A1_COD AND " + CRLF
		cQuerySF2	+=	"SF2.F2_LOJA = SA1.A1_LOJA " + CRLF
		
		cQuerySF2	+=	"LEFT JOIN " + RetSqlName("SA2") + " SA2 " + CRLF
		cQuerySF2	+=	"ON SF2.F2_CLIENTE = SA2.A2_COD AND " + CRLF
		cQuerySF2	+=	"SF2.F2_LOJA = SA2.A2_LOJA " + CRLF
		
		//---------- WHERE SF2
		
		cQuerySF2	+=	"WHERE SF2.F2_DAUTNFE = '" + aSyncEnv[9] + "' AND " + CRLF
		cQuerySF2	+=	"SF2.F2_FILIAL IN(" + cFiliais + ") AND " + CRLF
		cQuerySF2	+=	"SF2.F2_CHVNFE <> '" + Space(GetSx3Cache('F2_CHVNFE','X3_TAMANHO')) + "' AND " + CRLF
		cQuerySF2	+=	"(SD2.D2_SERIORI <> '' OR " + CRLF
		cQuerySF2	+=	"SD2.D2_NFORI <> '') AND " + CRLF
		cQuerySF2	+=	"SF2.F2_TIPO IN('C','I','P','D') AND " + CRLF
		cQuerySF2	+=	"ISNULL(SF2.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(SD2.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(SF1_ORI.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(SF2_ORI.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(ZB6.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(SA1.D_E_L_E_T_,'') <> '*' AND " + CRLF
		cQuerySF2	+=	"ISNULL(SA2.D_E_L_E_T_,'') <> '*' " + CRLF
		
		// ---------- UNION ALL
		
		cQuery	+=	cQuerySF1
		cQuery	+=	"UNION ALL" + CRLF
		cQuery	+=	cQuerySF2
		
		cQueryBkp	:=	cQuery
		
		cQuery := ChangeQuery(cQuery)
		
		If TcSqlExec(cQuery) < 0
			
			U_SYNCMAIL(AllTrim(aSyncEnv[15]),cLayout + ' - ' + AllTrim(aSyncEnv[17]),'Erro na execucao da query no banco de dados: ' + Chr(13) + Chr(10) + AllTrim(TcSqlError()),AllTrim(SubStr(aSyncEnv[4],1,6)),AllTrim(SubStr(aSyncEnv[4],7,6)),1,aSyncEnv[8],aSyncEnv[9])
			
		Else
			
			DbUseArea(.T.,"TOPCONN",TCGenQry(,,cQuery),cTrab,.F.,.T.)
			
			U_SYNCLOG(;
				'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
				'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
				cLayout + ' - ' + AllTrim(aSyncEnv[17]) + CRLF +;
				'Query executada com sucesso: ' + CRLF + cQueryBkp)
			
			// Cria arquivo temporario
			
			fErase(Separa(cTempFile,'\',.T.)[3])
			DbCreate(cTempFile,aStru,__LocalDriver)
			DbUseArea(.T.,__LocalDriver,cTempFile,cTempTrab,.T.,.F.)
			
			U_SYNCLOG(;
				'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
				'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
				'Arquivo Temporario criado com sucesso : ' + cTempFile)
			
			// --------------------------------------------------------------
			// Preenchimento do arquivo temporario
			// --------------------------------------------------------------
			
			(cTrab)->(DbGotop())
			
			Do While .Not. (cTrab)->(EOF())
				
				RecLock(cTempTrab,.T.)
				
				(cTempTrab)->POS0001 := (cTrab)->POS0001
				(cTempTrab)->POS0006 := (cTrab)->POS0006
				(cTempTrab)->POS0007 := (cTrab)->POS0007
				(cTempTrab)->POS0015 := (cTrab)->POS0015
				(cTempTrab)->POS0023 := (cTrab)->POS0023
				(cTempTrab)->POS0031 := (cTrab)->POS0031
				(cTempTrab)->POS0039 := (cTrab)->POS0039
				
				// DOF_IMPORT_NUMERO DOC ASSOCIADO
				(cTempTrab)->POS0047 := U_SYNCFIL(aSyncEnv[7],5,(cTrab)->FILIAL) + Str((cTrab)->RECNO,10) + (cTrab)->POS0031 + (cTrab)->POS0007 + (cTrab)->EMISSAO
				
				// DOF_IMPORT_NUMERO DOC ORIGEM
				(cTempTrab)->POS0087 := U_SYNCFIL(aSyncEnv[7],5,(cTrab)->FILIAL) + Str((cTrab)->RECNO_ORI,10) + (cTrab)->DOC_ORI + (cTrab)->SERIE_ORI + (cTrab)->EMISSAO_ORI
				
				(cTempTrab)->POS0127 := U_SYNCFIL(aSyncEnv[7],2,(cTrab)->FILIAL) + U_SYNCFIL(aSyncEnv[7],3,(cTrab)->FILIAL)
				
				// CNPJ Emitente Documento Associado
				If (cTrab)->ORIGEM = 'SF1'
					
					If (cTrab)->FORMUL == 'S'
						
						(cTempTrab)->POS0145 := U_SYNCFIL(aSyncEnv[7],2,(cTrab)->FILIAL)
						
					Else
						
						If (cTrab)->TIPO $ 'CIP'
							
							(cTempTrab)->POS0145 := (cTrab)->SA2_CGC
							
						ElseIf (cTrab)->TIPO == 'D'
							
							(cTempTrab)->POS0145 := (cTrab)->SA1_CGC
							
						End If
						
					End If
					
				ElseIf (cTrab)->ORIGEM = 'SF2'
					
					(cTempTrab)->POS0163 := U_SYNCFIL(aSyncEnv[7],2,(cTrab)->FILIAL)
					
				End If
				
				// CNPJ Destinatario
				If (cTrab)->ORIGEM = 'SF1'
					
					(cTempTrab)->POS0163 := U_SYNCFIL(aSyncEnv[7],2,(cTrab)->FILIAL)
					
				ElseIf (cTrab)->ORIGEM = 'SF2'
					
					If (cTrab)->TIPO $ 'CIP'
						
						(cTempTrab)->POS0163 := (cTrab)->SA1_CGC
						
					ElseIf (cTrab)->TIPO == 'D'
						
						(cTempTrab)->POS0163 := (cTrab)->SA2_CGC
						
					End If
					
				End If
				//----------------
				
				(cTempTrab)->POS0181 := '+' + StrTran(Transform((cTrab)->POS0181,'@E '+ Replicate('9',17) + '.' + Replicate('9',2)),' ','0')
				
				MsUnLock()
				
				(cTrab)->(DbSkip())
				
				nCount++
				
			End Do
			
			// --------------------------------------------------------------
			
			U_SYNCDAT(cTempTrab,aSyncEnv,nCount)
			
			(cTrab)->(DbCloseArea())
			(cTempTrab)->(DbCloseArea())
			fErase(Separa(cTempFile,'\',.T.)[3])
			
		End IF
		
	Else
		
		U_SYNCLOG(;
			'Agenda : ' +  SubStr(aSyncEnv[4],1,6) + CRLF +;
			'Tarefa : ' +  SubStr(aSyncEnv[4],7,6) + CRLF +;
			'Nao foi possivel montar o ambiente de geracao do layout ' + cLayout)
		
	End IF
	
	RpcClearEnv()
	
Return
